package com.zh.note.huawei.twohundred.服务依赖;

/**
 * https://dream.blog.csdn.net/article/details/129910888
 * <p>
 * a1-a2,a5-a6,a2-a3
 * a5,a2
 */

import java.util.*;

public class Main {
    public static List<String[]> list = new LinkedList<>();
    public static List<String> errorList = new LinkedList<>();
    public static Set<String> errorSet = new LinkedHashSet<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // String line1 = sc.nextLine();
        // String line2 = sc.nextLine();
        String line1 = "a1-a2,a5-a6,a2-a3,a7-a1";
        String line2 = "a5,a2";
        String[] strs = line1.split(",");
        String[] err = line2.split(",");
        errorList = Arrays.asList(err);
        errorSet.addAll(errorList);
        //找到不直接出现在line2里面的元素
        // 1、保存正常的
        List<String> tempList = new LinkedList<>();
        for (String str : strs) {
            String[] temp = str.split("-");
            list.add(temp);
            String a = temp[0];
            String b = temp[1];
            if (!errorSet.contains(a) && !tempList.contains(a)) {
                tempList.add(a);
            }
            if (!errorSet.contains(b) && !tempList.contains(b)) {
                tempList.add(b);
            }
        }
        //2、过滤掉 关联关系导致失败的
        List<String> resList = new LinkedList<>();
        for (String v : tempList) {
            if (!help(v)) {
                resList.add(v);
            }
        }

        int len = resList.size();
        if (len == 0) {
            System.out.println(",");
        } else {
            String res = String.join(",", resList);
            System.out.println(res);
        }
    }

    // 判断s是否存在关联关系 递归
    public static boolean help(String s) {
        // 先判断是否是错误集合里面的
        if (errorSet.contains(s)) {
            return true;
        }
        for (String[] arr : list) {
            if (arr[0].equals(s) && help(arr[1])) {
                return true;
            }
        }
        return false;
    }
}

